package com.fzshang.commen.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.RoyaltyInfo;
import com.alipay.api.request.*;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.alipay.api.response.AlipayOpenAgentConfirmResponse;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.fzshang.commen.service.AliPayService;
import com.fzshang.commen.util.zhifubao.AliPayConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author 张子艺
 * @packge com.xbxkj.service.serviceimpl
 * @data 2019-05-02 15:08
 * @project GoodLuck
 */
@Service
public class AliPayServiceImpl extends AliPayConfig implements AliPayService {

    @Override
    public String MaChinePCPay(String moeny, String subject, String body, String OrderNo) {
      return  pcPay(moeny,subject,body,OrderNo,RechargeReturnUrl(),MaChineNotify());
    }

    @Override
    public String PayMemtPCPay(String moeny, String subject, String body, String OrderNo) {
        return  pcPay(moeny,subject,body,OrderNo,"",PayMentNotifyUrl());
    }

    @Override
    public String MaChineAPPPay(String OrderNo, String moeny,String subject) throws AlipayApiException {
        return h5Pay(OrderNo,subject,moeny,RechargeReturnUrl(),MaChineNotify());
    }

    @Override
    public String RechargeH5Pay( String OrderNo,String moeny,String subject) throws AlipayApiException {
       return h5Pay(OrderNo,moeny,subject,RechargeReturnUrl(),RechargeNotifyUrl());
    }












/*    @Override
    public String Refound(RefoundBean refoundBean) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(ALIURL, APPID, RSA2_Private_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY,SIGNTYPE);
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        //把封装类转成JSON
        String refoundJson= JSONObject.toJSONString(refoundBean);

        request.setBizContent(refoundJson);
        //退款
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            return "success";
        } else {
            System.err.println(response.getMsg());
            return "fail";
        }
    }*/

    @Override
    public String Transfer(String moeny, String account, String orderNum)   {
        AlipayClient alipayClient = new DefaultAlipayClient(ALIURL, APPID, RSA2_Private_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY,SIGNTYPE); //获得初始化的AlipayClient
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();


        //收款方
        //String payeeAccount = "dbbits1353@sandbox.com";
        request.setBizContent("{" +
                "    \"out_biz_no\":\""+orderNum+"\"," +
                "    \"payee_type\":\"ALIPAY_LOGONID\"," +
                "    \"payee_account\":\""+account+"\"," +
                "    \"amount\":\""+moeny+"\"," +
                "    \"payer_show_name\":\"提现\"," +
                "    \"remark\":\"转账备注\"," +
                "  }");


        try {
            AlipayFundTransToaccountTransferResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                System.out.println("调用成功");
                return "success";

            } else {
                System.out.println("调用失败");
                return response.getSubMsg();
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "fail";
    }


    private String pcPay(String moeny, String subject, String body, String OrderNo,String returnUrl,String notifyUrl){
        AlipayClient alipayClient = new DefaultAlipayClient(ALIURL, APPID, RSA2_Private_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY,SIGNTYPE); //获得初始化的AlipayClient
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
        //回调地址
         alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                "    \"out_trade_no\":\""+OrderNo+"\"," +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "    \"total_amount\":"+moeny+"," +
                "    \"subject\":\""+subject+"\"," +
                "    \"body\":\""+body+"\"," +
                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," +
                "    \"extend_params\":{" +
                "    \"sys_service_provider_id\":\"2088511833207846\"" +
                "    }"+
                "  }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return form;
    }

    private  String h5Pay(String OrderNo,String moeny,String subject,String returnUrl,String notifyUrl) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(ALIURL, APPID, RSA2_Private_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY,SIGNTYPE); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
        // TODO: 2019/1/2 支付成功回调
          alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                "\"out_trade_no\":\""+OrderNo+"\"," +
                "\"total_amount\":"+moeny+"," +
                "\"subject\":\""+subject+"\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form="";

        form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        return  form;
    }

    private  String APPPay(String OrderNo,String moeny,String subject,String returnUrl,String notifyUrl) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(ALIURL, APPID, RSA2_Private_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY,SIGNTYPE); //获得初始化的AlipayClient
        AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();//创建API对应的request
        AlipayTradeAppPayModel model=new AlipayTradeAppPayModel();
        model.setBody(subject);
        model.setSubject(subject);
        model.setOutTradeNo(OrderNo);
        model.setTotalAmount(moeny);
        model.setProductCode("QUICK_MSECURITY_PAY");
        model.setRoyaltyInfo(new RoyaltyInfo());
        alipayRequest.setBizModel(model);
        AlipayTradeAppPayResponse response=alipayClient.sdkExecute(alipayRequest);
        System.err.println(response.getBody());
        return response.getBody();
    }
    @Value("${alipay.APPID}")
    protected String APPID = "";
    @Value("${alipay.NotifyURL}")
    protected String NotifyURL = "";
    @Value("${alipay.MaChineNotify}")
    protected String MaChineNotify = "";
    @Value("${alipay.RechargeNotifyUrl}")
    protected String RechargeNotifyUrl = "";
    @Value("${alipay.RechargeReturnUrl}")
    protected String RechargeReturnUrl = "";
    @Value("${alipay.PayMentNotifyUrl}")
    protected String PayMentNotifyUrl = "";
    @Value("${alipay.ALIURL}")
    protected String ALIURL = "";
    @Value("${alipay.CHARSET}")
    protected String CHARSET = "";
    @Value("${alipay.FORMAT}")
    protected String FORMAT = "";
    @Value("${alipay.RSA2_Private_KEY}")
    protected String RSA2_Private_KEY = "";
    @Value("${alipay.RSA2_PUBLIC_KEY}")
    protected String RSA2_PUBLIC_KEY = "";
    @Value("${alipay.ALIPAY_PUBLIC_KEY}")
    protected String ALIPAY_PUBLIC_KEY = "";
    @Value("${alipay.SIGNTYPE}")
    protected String SIGNTYPE = "";

    @Override
    public String getAppID() {
        return APPID;
    }

    @Override
    public String getNotifyURL() {
        return NotifyURL;
    }

    @Override
    public String MaChineNotify() {
        System.err.println(NotifyURL+MaChineNotify);
        return NotifyURL+MaChineNotify;
    }

    @Override
    public String RechargeNotifyUrl() {
        return NotifyURL+RechargeNotifyUrl;
    }

    @Override
    public String RechargeReturnUrl() {
        return NotifyURL+RechargeReturnUrl;
    }

    @Override
    public String PayMentNotifyUrl() {
        return NotifyURL+PayMentNotifyUrl;
    }

    @Override
    public String ALIURL() {
        return ALIURL;
    }

    @Override
    public String CHARSET() {
        return CHARSET;
    }

    @Override
    public String FORMAT() {
        return FORMAT;
    }

    @Override
    public String RSA2_Private_KEY() {
        return RSA2_Private_KEY;
    }

    @Override
    public String RSA2_PUBLIC_KEY() {
        return RSA2_PUBLIC_KEY;
    }

    @Override
    public String ALIPAY_PUBLIC_KEY() {
        return ALIPAY_PUBLIC_KEY;
    }

    @Override
    public String SIGNTYPE() {
        return SIGNTYPE;
    }


    public  static  void main(String args[]) throws AlipayApiException {




        String   RSA2_Private_KEY="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCHPX62BJbpLWoQR1S2JR20ES1j3OIOrzQ1DYJEDWlBPX8ERLru6Lo5aSj74iM/pyYgOXyJcPDyUn9VYsaxBeezbPmkSkspErpgn/DBwnuxtz/iQc2pOCY/cNy6nTXiGTt16MlT9XtkuQsCjt1HqaEBtV8cQKLFO/bB8zBllttVRWemWXDfo6CHXAfwJgRJS/N2H+oqUsU+3dhih1F+4ft0ShhZZxnRBpUqb82Y2OKjB9sUxYQ4c7gWQAtwVVDJ9UfcbudQMngQ7TW+KA8QwkOY0hRb2Gs7D2nd358r0RcWZvv4IGDlXh5t7NYPSL7oLOSt5F3+BhIN3cGVHgqHyt/bAgMBAAECggEAHO91WvBkcYN7+/unDkmmr041jfZGj6nA29EGHBVafdDwijNCY7d7kKkFUc8yh3311FM4aBe4FcC86cH2jQ2cY3FfgF08847EPcQ6uNopqP5yNrCplhkjZdN6Bpt4WeXYKQQjf0k4m3ZT+roiciDDCCCMNvlFWjnYM+saYcIDL50YMVRxWEMbtkLPG1Jsf9SaWZodTQvsiJIFNMaYzTCpvyzcstn4WYVQ+T4xFGHGqEEc3MTIdUAzUk/nr4+OubMBbV1bBApRLUyO4XNLzE7HijNITSPbnC9qroLvkI6ZO6vGge0tZyKOKonqUnCpZis88DchC74esLsmh1am5oa8wQKBgQDEXwI9cF2Z1IiJEeRm2mXXJl6kE4+PJtRaKu9kbOH0jyUsBKzcBnAV2Dq/63WBXG6MQxtzB2QhcsgNy8J5YqBHrvc2C7kaqoJFUrrYYlaQ5HgYljCNewUJQEFMC4YdgMH+j8zwOGBi2ITuSHW/uydZamzA2Tf8GMn3g+r3v2rk0QKBgQCwTnMDJnTNF0RMjKfuO/EPvzlQxC6R/aCXnczFBK6oEjEC+ZODuZ1tDcuO9ov28kFvSDte5JakuT7F9/xJ7N5rRDzvN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AlipayClient("https://openapi.alipay.com/gateway.do",appid,RSA2_Private_KEY,"json","UTF-8",ALIPAY_PUBLIC_KEY,"RSA2");
        AlipayOpenAgentConfirmRequest request = new AlipayOpenAgentConfirmRequest();
        request.setBizContent("{" +
                "\"batch_no\":\"2019071018423420000088926\"" +
                "  }");
        AlipayOpenAgentConfirmResponse  response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
            System.out.println(response.getAppAuthToken());
        } else {
            System.out.println("调用失败");

        }

/*        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "2019071065815220", RSA2_Private_KEY, "json", "UTF-8", ALIPAY_PUBLIC_KEY,"RSA2"); //获得初始化的AlipayClient
        AlipayOpenAgentCreateRequest alipayRequest = new AlipayOpenAgentCreateRequest();//创建API对应的request
        AlipayOpenAgentCreateModel model=new AlipayOpenAgentCreateModel();
        model.setAccount("18602151637");
        ContactModel contactModel=new ContactModel();
        contactModel.setContactEmail("1832870214@qq.com");
        contactModel.setContactMobile("18602151637");
        contactModel.setContactName("张子艺");
        model.setContactInfo(contactModel);
        AlipayOpenAgentCreateResponse  response=alipayClient.sdkExecute(alipayRequest);
        System.err.println(response.getCode());*/
    }
}
